package com.freen.fadmin.controller.jwt.SysuserApi;

import com.alibaba.fastjson.JSON;
import com.freen.fadmin.controller.jwt.JwtSysuserBaseController;
import com.freen.fadmin.entity.sysuser.SysRole;
import com.freen.fadmin.entity.sysuser.SysUser;
import com.freen.fadmin.repository.sysuser.SysPermissionRepository;
import com.freen.fadmin.repository.sysuser.SysRoleRepository;
import com.freen.fadmin.repository.sysuser.SysUserRepository;
import com.freen.fadmin.util.JwtSysuserTokenUtils;
import com.freen.fadmin.util.OBJtoHashMap;
import com.freen.fadmin.util.result.ExceptionMsg;
import com.freen.fadmin.util.result.Response;
import com.freen.fadmin.util.result.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class MainController extends JwtSysuserBaseController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    private SysUserRepository sysUserRepository;
    @Autowired
    private SysRoleRepository sysRoleRepository;
    @Autowired
    private SysPermissionRepository sysPermissionRepository;

    //反馈前端当前用户信息接口
    @ResponseBody
    @RequestMapping(value = "/SysApi/SysUser",method = RequestMethod.GET)
    //@PreAuthorize("principal.id.equals(#user_id)")
    public Response Sysuser(Principal principal){
        //Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser sysUser = sysUserRepository.findByName(principal.getName());
        HashMap<String,Object> resSysUser;
        try {
            resSysUser = OBJtoHashMap.convertToMap(sysUser);
            resSysUser.remove("password");
            resSysUser.remove("roles");
            ArrayList <String> roles = new ArrayList<>();
            for (SysRole role:sysUser.getRoles()){
                String srole = role.getRole();
                String[] strs = srole.split("_");
                roles.add(strs[1]);
            }
            resSysUser.put("roles",roles);
        }catch (Exception e){
            return result();
        }
        
        return new ResponseData(ExceptionMsg.SUCCESS,resSysUser);
    }


    // 用户登出操作
    @ResponseBody
    @RequestMapping(value = "/SysApi/Logout",method = RequestMethod.GET)
    public Response Logout(HttpServletRequest request){
        String tokenHeader = request.getHeader(JwtSysuserTokenUtils.TOKEN_HEADER);
        String token = tokenHeader.replace(JwtSysuserTokenUtils.TOKEN_PREFIX,"");
        ServletContext context = request.getServletContext();
        context.removeAttribute(token);
        return result(ExceptionMsg.SUCCESS);
    }

}
